<template>
  <div class="login-container">
    <div class="login-form-wrap">
      <div class="login-head">
        <div class="logo"></div>
      </div>
      <el-form :model="user" :rules="rules" ref="user" class="login-form">
      <el-form-item prop="mobile">
        <el-input prefix-icon='el-icon-mobile-phone' v-model="user.mobile" placeholder="请输入手机号"></el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-input prefix-icon='el-icon-circle-check' v-model="user.code" placeholder="请输入验证码"></el-input>
      </el-form-item>
      <el-form-item prop="agree">
        <el-checkbox v-model="user.agree">我已阅读并同意用户协议和隐私条款</el-checkbox>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :loading="loading" @click="submitForm()" class="login-btn">登录</el-button>
      </el-form-item>
    </el-form>
    </div>
  </div>
</template>

<script>
import { login } from '@/api/user.js'
export default {
  name: 'LoginIndex',
  components: {},
  props: {},
  data () {
    return {
      user: {
        mobile: '13911111111',
        code: '246810',
        agree: false
      },
      loading: false,
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'change' },
          { pattern: /^1[3|5|7|8|9]\d{9}$/, message: '请输入正确的号码格式', trigger: 'change' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'change' },
          { min: 6, max: 6, message: '请输入正确的验证码格式', trigger: 'change' }
        ],
        agree: [
          {
            validator: (rule, value, callback) => {
              if (value) {
                callback()
              } else {
                callback(new Error('请勾选同意用户协议'))
              }
            },
            trigger: 'change'
          }
        ]
      }
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    submitForm () {
      this.$refs.user.validate((valid) => {
        if (valid) {
          this.onLogin()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    onLogin () {
      this.loading = true
      login(this.user).then(res => {
        console.log(res)
        this.$message({
          message: '登录成功',
          type: 'success'
        })
        this.loading = false
        window.localStorage.setItem('user', JSON.stringify(res.data.data))
        this.$router.push({ name: 'home' })
      }).catch(err => {
        console.log('登录失败', err)
        this.$message.error('登录失败' + err)
        this.loading = false
      })
    }
  }
}

</script>
<style lang='less' scoped>
.login-container {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  background: url("./img/login_bg.jpg") no-repeat;
  background-size: cover;
  .login-form-wrap{
    min-width: 300px;
    background: #ffffff;
    padding: 30px 50px 10px;
    .login-head {
      display: flex;
      justify-content: center;
      .logo {
        // width: 200px;
        // height: 57px;
        width: 200px;
        height: 51px;
        background: url('./img/logo.png') no-repeat;
        background-size: contain;
      }
    }
    .login-form {
      .login-btn {
        width: 100%;
      }
    }
  }
}
</style>
